/*
 * Copyright 2009 Sven Meier
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package sk.fiit.icp.event.components;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.injection.Injector;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.fiit.icp.data.Event;
import sk.fiit.icp.data.Events;
import sk.fiit.icp.event.view.RegistrationForm;
import sk.fiit.icp.service.EventService;
import wickettree.util.SortableTreeProvider;

/**
 * @author Sven Meier
 */
public class FooTreeProvider extends SortableTreeProvider<Event> {

	@SpringBean(name = "eventService")
	EventService eventService;

	private List<Event> events;
	private Event m;

	public FooTreeProvider(RegistrationForm regForm) {
		super();
		Injector.get().inject( this );
		events = eventService.getEvents();

		if ( regForm.getModel().getObject().getIdevent() == null ) {
			m = new Event( regForm.getModel().getObject(), null, null );
			events.add( m );
		}

	}

	public Iterator<? extends Event> getRoots() {
		List<Event> finalEvents = new LinkedList<Event>();

		for ( Event eventFinal : events ) {
			if ( eventFinal.getParent() == null ) {
				finalEvents.add( eventFinal );
			}
		}
		return finalEvents.iterator();
	}

	public boolean hasChildren(Event foo) {
		return foo.hasChildren();
	}

	public Iterator<? extends Event> getChildren(Event foo) {
		return foo.getChildrens().iterator();
	}

	public IModel<Event> model(Event event) {
		return Model.of( event );
	}

	public void detach() {
	}

	public void remove(Event event) {
		events.remove( event );
	}

	public void add(Event event) {
		events.add( event );
	}

	public void add(Event drag, Event parent) {
		parent.add( drag );
	}

	public void addBefore(Event drag, Event drop) {
		Event parent = drop.getParent();
		if ( parent == null ) {
			drag.remove();
			events.add( events.indexOf( drop ), drag );
		} else {
			parent.add( drag, parent.indexOf( drop ) );
		}
	}

	public void addAfter(Event drag, Event drop) {
		Event parent = drop.getParent();
		if ( parent == null ) {
			drag.remove();
			events.add( events.indexOf( drop ) + 1, drag );
		} else {
			parent.add( drag, parent.indexOf( drop ) + 1 );
		}

	}

	public Events getModelParent() {
		for ( Event e : getEvents() ) {
			for ( Event ch : e.getChildrens() ) {
				if ( ch.equals( m ) ) {
					return e.getEventDB();
				}

			}
		}
		return null;
	}

	public List<Event> getEvents() {
		return events;
	}
}
